package com.yudi.blog.bussiness.controller;

import com.yudi.blog.bussiness.dto.Tag;
import com.yudi.blog.bussiness.service.BlogService;
import com.yudi.blog.bussiness.service.TagService;
import com.yudi.blog.bussiness.vo.BlogQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Author YuDi
 * @Date 2021/11/15 12:11
 * @Version 1.0
 */
@Controller
public class TagController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String Tags(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                       @PathVariable Long id,
                       Model model){
        List<Tag> tagsList = tagService.listTagTop(10000);
        if(id == -1){
            id = tagsList.get(0).getId();
        }

        model.addAttribute("tags", tagsList);
        model.addAttribute("page", blogService.listBlog(pageable, id));
        model.addAttribute("activeTagId", id);

        return "tags";
    }

}
